package md5p

import (
	"crypto/sha512"
	"fmt"
	"strings"

	"github.com/anaskhan96/go-password-encoder"
)

// func Init() {
// 	options := &password.Options{SaltLen: 16, Iterations: 100, KeyLen: 32, HashFunction: sha512.New}
// 	salt, encodedPwd := password.Encode("eE0@lC0aE7@bD3$eA4$", options)

// 	newPassword := fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodedPwd) // pbkdf2-sha512是算法，使用$来拼接
// 	fmt.Println(len(newPassword))                                        // 要确保长度不会超过数据库字段的定义
// 	fmt.Println(newPassword)

// 	//解析
// 	passwordInfo := strings.Split(newPassword, "$")
// 	fmt.Println(passwordInfo)

//		check := password.Verify("eE0@lC0aE7@bD3$eA4$", passwordInfo[2], passwordInfo[3], options)
//		fmt.Println(check) // true
//	}
func Md5password(psword string) string {
	options := &password.Options{SaltLen: 16, Iterations: 100, KeyLen: 32, HashFunction: sha512.New}
	salt, encodedPwd := password.Encode(psword, options)
	newPassword := fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodedPwd) // pbkdf2-sha512是算法，使用$来拼接
	return newPassword
}
func VerifyPassword(psword, newPassword string) bool {
	options := &password.Options{SaltLen: 16, Iterations: 100, KeyLen: 32, HashFunction: sha512.New}
	passwordInfo := strings.Split(newPassword, "$")
	fmt.Println(passwordInfo,"passwordInfo")
	check := password.Verify(psword, passwordInfo[2], passwordInfo[3], options)
	return check
}
